/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

#pragma once

#include <ctime>
#include "common/type/data_type.h"

/**
 * @brief 日期类型
 * @ingroup DataType
 */
class DateType : public DataType
{
public:
  DateType() : DataType(AttrType::DATES) {}
  virtual ~DateType() = default;

  int compare(const Value &left, const Value &right) const override;

  /**
   * @brief 从字符串中解析日期
   * @param date_str 日期字符串，格式为YYYY-MM-DD
   * @param date 输出参数，解析后的日期值(int32_t)
   * @return RC::SUCCESS 如果解析成功，RC::FAILURE 如果日期无效
   */
  static RC parse_date(const char *date_str, int32_t &date);

  /**
   * @brief 日期值转换为字符串
   * @param date 日期值(int32_t)
   * @param str 输出参数，日期字符串
   */
  static void date_to_string(int32_t date, string &str);

  /**
   * @brief 判断是否为闰年
   * @param year 年份
   * @return 是闰年返回true，否则返回false
   */
  static bool is_leap_year(int year);

  /**
   * @brief 判断日期是否有效
   * @param year 年份
   * @param month 月份
   * @param day 天
   * @return 日期有效返回true，否则返回false
   */
  static bool is_valid_date(int year, int month, int day);

  RC set_value_from_str(Value &val, const string &data) const override;
  RC cast_to(const Value &val, AttrType type, Value &result) const override;
  RC to_string(const Value &val, string &result) const override;
}; 